//Copyright (c) 2014 MagicCard
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;

namespace MCGioiThieuHang
{
    public class MGioiThieuHang
    {
        private Texture2D[] manganh;
        private Texture2D anhhienthi, anhnen;
        private int tongsoanh;
        private int rongmanhinh, daimanhinh;
        private float tilerong, tiledai;
        private MouseState trochuothientai, trochuottruocdo;
        private int bienchay = 0, hoatcanh = 0, thoigianchay = 0, thoigianbatdau = 20;
        private Rectangle boanhnen, boanhhienthi;
        private SoundEffect amchay;
        public MGioiThieuHang(Texture2D[] danhsachanh, Texture2D anhnengioithieu, SoundEffect amthanh, int rongmanhinhchuan, int daimanhinhchuan, float tiledanrong, float tiledandai)
        {
            rongmanhinh = rongmanhinhchuan;
            daimanhinh = daimanhinhchuan;
            tilerong = tiledanrong;
            tiledai = tiledandai;
            manganh = danhsachanh;
            tongsoanh = manganh.Length;
            anhnen = anhnengioithieu;
            boanhnen = new Rectangle(0, 0, Convert.ToInt32(rongmanhinh * tilerong), Convert.ToInt32(daimanhinh * tiledai));
            anhhienthi = manganh[0];
            boanhhienthi = new Rectangle(Convert.ToInt32((rongmanhinh - anhhienthi.Width) / 2 * tilerong), Convert.ToInt32((daimanhinh - anhhienthi.Height) / 2 * tiledai), Convert.ToInt32(anhhienthi.Width * tilerong), Convert.ToInt32(anhhienthi.Height * tiledai));
            amchay = amthanh;
        }
        public Boolean ChayGioiThieu(int tocdo, int thoigian, float kichthuocamthanh)
        {
            Boolean ketqua = false;
            if (thoigian < tongsoanh + thoigianbatdau) thoigian = tongsoanh + thoigianbatdau;
            bienchay++;
            if (bienchay == tocdo)
            {
                if (thoigianchay >= thoigianbatdau)
                {
                    if (hoatcanh == 0) amchay.Play(kichthuocamthanh, 0, 0);
                    if (hoatcanh < tongsoanh - 1) hoatcanh++;
                    else hoatcanh = tongsoanh - 1;
                    anhhienthi = manganh[hoatcanh];
                }
                thoigianchay++;
                bienchay = 0;
            }
            trochuothientai = GetMouseState();
            int baoboqua = 0;
            if (trochuothientai.LeftButton == ButtonState.Pressed && trochuottruocdo.LeftButton == ButtonState.Released)
            {
                baoboqua = 1;
            }
            trochuottruocdo = trochuothientai;
            if ((thoigianchay == thoigian + thoigianbatdau) | (baoboqua == 1))
            {
                bienchay = 0;
                hoatcanh = 0;
                thoigianchay = 0;
                ketqua = true;
            }
            return ketqua;
        }
        public void HienThiGioiThieu(SpriteBatch nenve)
        {
            nenve.Draw(anhnen, boanhnen, Color.White);
            nenve.Draw(anhhienthi, boanhhienthi, Color.White);
        }
        private MouseState GetMouseState()
        {
            TouchCollection TouchState = TouchPanel.GetState();
            MouseState CurrentMouseState = new MouseState();
            foreach (TouchLocation tl in TouchState)
            {
                CurrentMouseState = new MouseState((int)tl.Position.X, (int)tl.Position.Y, 0, ButtonState.Pressed, ButtonState.Released, ButtonState.Released, ButtonState.Released, ButtonState.Released);
            }
            return CurrentMouseState;
        }
    }
}